package cn.arikeky.controller;


import cn.arikeky.domain.User;
import cn.arikeky.dto.BuyDto;
import cn.arikeky.feign.MoveService;
import cn.arikeky.query.UserQuery;
import cn.arikeky.result.JsonResult;
import cn.arikeky.result.PageList;
import cn.arikeky.service.IUserService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 购买电影票
     */
    @RequestMapping(value = "/buy/{movieId}/{count}",method = RequestMethod.GET)
    public JsonResult buyMovie(@PathVariable Long movieId,@PathVariable Integer count ){
        return userService.buyMovie(movieId,count);
    }

    /**
     * 观看电影票
     */
    @RequestMapping(value = "/watch/{movieId}/{count}",method = RequestMethod.GET)
    public JsonResult watch(@PathVariable Long movieId,@PathVariable Integer count ){
        return userService.watch(movieId,count);
    }

    /**
     * 查询所有电影票
     */
    @RequestMapping(value = "/lists",method = RequestMethod.GET)
    public JsonResult lists(){
        return userService.getMovie();
    }


    /**
     * 保存和修改公用的
     */
    @RequestMapping(value="/give",method= RequestMethod.POST)
    public JsonResult give(@RequestBody User user){
        return userService.give(user);
    }
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JsonResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }
}
